package com.example.mannortycoon.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.mannortycoon.entity.UserPointingEntity;
import com.example.mannortycoon.service.UserPointingService;
import com.example.mannortycoon.uitl.JWTUtils;
import com.example.mannortycoon.uitl.PageUtils;
import com.example.mannortycoon.uitl.R;
import com.example.mannortycoon.vo.UserPointVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Path;


@RestController
@RequestMapping("game/userPointing")
@Api(tags="用户种植")
public class UserPointingController {

    private final UserPointingService userPointingService;

    @Autowired
    public UserPointingController(UserPointingService userPointingService){
        this.userPointingService = userPointingService;
    }

    /**
     * app获取用户种植列表
     */
    @GetMapping("/getUserPointList")
    @ApiOperation("获取用户种植列表")
    public R getUserPointList(HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        List<UserPointVo> list = userPointingService.getUserPointList(userId);
        return R.ok().put("data",list);
    }

    /**
     * 用户种植
     */
    @PostMapping("/userPoint/{number}/{seedId}")
    @ApiOperation("用户种植")
    public R userPoint(@PathVariable("number") Integer number,@PathVariable("seedId") Long seedId,HttpServletRequest request){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        return userPointingService.userPoint(userId,seedId,number);
    }

    /**
     * 用户收获作物
     */
    @ApiOperation("用户收获作物")
    @PutMapping("/harvest/{number}")
    public R harvest(HttpServletRequest request,@PathVariable("number") Integer number){
        Long userId = JWTUtils.getId(request.getHeader("token"));
        Integer count = userPointingService.harvest(userId,number);
        if(count == null){
            return R.error("种植地信息有误");
        }
        return R.ok().put("data",count);
    }

    /**
     * 后台获取用户种植地
     */
    @GetMapping("/info/{id}")
    @ApiIgnore
    public R info(@PathVariable("id") Long id){
        List<UserPointingEntity> pointList = userPointingService.info(id);
        return R.ok().put("pointList", pointList);
    }

    /**
     * 保存
     */
    @PostMapping("/save/{userId}")
    @ApiIgnore
    public R save(@PathVariable("userId") Long userId,@RequestBody List<UserPointingEntity> pointList){
		userPointingService.saveUserPointing(userId,pointList);
        return R.ok();
    }
}
